class _root extends MovieClip
{
	// implement empty objects
	var kongregateServices = { connect: function() { } }
	var kongregateStats = { submit: function() { } }
	var kongregateScores = { submit: function() { } }
	
	// declare global variables
	var background;
	var ship;
	var scoreText;
	var healthMeter;
	var enemyHealthMeter;
	var gameOverMenu;
	var playMenu;	
	
	function init() {
		// attach all movies
		j.getClass('RewardPoints').prototype.field = {}
	
		background = attachMovie("Background","background", _root.getNextHighestDepth());
		
		scoreText = attachMovie("DynamicText","scoreText", _root.getNextHighestDepth());
		scoreText._x = 344;
		scoreText._y = 10;
		scoreText.font = 'bold 20px Comic Sans MS Bold';
		scoreText.type = 'stroke';
		
		healthMeter = attachMovie("Bar","healthMeter", _root.getNextHighestDepth());
		healthMeter.bar = {}
		healthMeter._x = 30;
		healthMeter._y = 10;
		healthMeter._width = 55;
		healthMeter._height = 15;

		enemyHealthMeter = attachMovie("Bar","enemyHealthMeter", _root.getNextHighestDepth());
		enemyHealthMeter.bar = {}
		enemyHealthMeter._x = 530;
		enemyHealthMeter._y = 10;
		enemyHealthMeter._width = 55;
		enemyHealthMeter._height = 15;
		enemyHealthMeter._visible = false;
		
		gameOverMenu = attachMovie("MovieClip","gameOverMenu", _root.getNextHighestDepth());
		gameOverMenu.killBonus = {}
		gameOverMenu.hitBonus = {}
		gameOverMenu.finalScore = {}		
		gameOverMenu._visible = false;
		gameOverMenu.playAgainButton = j.getNestedMovieClip(gameOverMenu, "MovieClip", "gameOverMenuPlayAgainButton");

		playMenu = attachMovie("MovieClip","playMenu", _root.getNextHighestDepth());	
		playMenu.playButton = j.getNestedMovieClip(playMenu, "MovieClip", "playMenuPlayButton");

		ship = attachMovie("Ship","ship", _root.getNextHighestDepth());
		ship._visible = false;
	}
	
	// custom drawing for certain objects i didn't have sprites for
	function customDraw(m) {
		if (m._name == 'Missile' || m._name == 'EnemyMissile') {
			m._width = 8;
			m._height = 6;
		
			j.ctx.save();
			j.ctx.fillStyle = '#000';
			j.ctx.fillRect(m._x - m._width / 2.0, m._y - m._height / 2.0, m._width, m._height);
			j.ctx.fillStyle = (m._name == 'Missile' ? '#fff' : '#f00');
			j.ctx.fillRect(m._x - m._width / 2.0 + 1, m._y - m._height / 2.0 + 1, m._width - 2, m._height - 2); 
			j.ctx.restore();
		}
		if (m._name == 'Explosion' || m._name == 'SmallExplosion') {
			m.radius = (m.radius || 0) + 1;
			
			j.ctx.save();
			j.ctx.fillStyle = '#f00';
			j.ctx.beginPath();
			j.ctx.arc(m._x - 30, m._y, m.radius * 2, 0, Math.PI*2, true); 
			j.ctx.closePath();
			j.ctx.fill();
			j.ctx.restore();
			
			if (m._name == 'Explosion' && m.radius == 10) m.removeMovieClip();
			if (m._name == 'SmallExplosion' && m.radius == 5) m.removeMovieClip();
		}
		if (m._name == 'Bar') {
			j.ctx.save();
			j.ctx.fillStyle = '#ddd';
			j.ctx.fillRect(m._x - m._width / 2.0, m._y - m._height / 2.0, m._width, m._height);
			j.ctx.fillStyle = '#3f0';
			j.ctx.fillRect(m._x - m._width / 2.0, m._y - m._height / 2.0, m._width * (m.bar._xscale / 100.0), m._height);
			j.ctx.strokeStyle = '#000';
			j.ctx.lineWidth = 2;
			j.ctx.strokeRect(m._x - m._width / 2.0, m._y - m._height / 2.0, m._width, m._height);
			j.ctx.restore();
		}
		if (m._name == 'RewardPoints') {
			if (m.field._name != 'DynamicText') {
				var text = m.field.text;
				m.field = j.getNestedMovieClip(m, 'DynamicText', m._id + 'DynamicText');
				m.field.text = text;
				m.field.type = 'stroke';
				m.field.font = 'bold 15px Comic Sans MS Bold';
			}
		}
		if (m._id == 'playMenu' || m._id == 'gameOverMenu') {
			m._x = 50; 
			m._y = 50;
			m._width = 200;
			m._height = 100;
			if (m._id == 'playMenu') {
				m.playButton._width = m._width;
				m.playButton._height = m._height;
			}
			if (m._id == 'gameOverMenu') {
				m.playAgainButton._width = m._width;
				m.playAgainButton._height = m._height;			
			}
			
			j.ctx.save();
			j.ctx.fillStyle = '#ddd';
			j.ctx.fillRect(m._x - m._width / 2.0, m._y - m._height / 2.0, m._width, m._height);
			j.ctx.fillStyle = '#000';			
			if (m._id == 'playMenu') j.ctx.fillText("start!", m._x, m._y);
			if (m._id == 'gameOverMenu') j.ctx.fillText("play again?", m._x, m._y);
			j.ctx.restore();
		}
		if (m._id == 'shield') {
			j.ctx.save();
			j.ctx.fillStyle = '#3f0';
			j.ctx.fillRect(m._x + m._parent._width / 2.0 + 5, m._y - m._parent._height / 2.0, 10, m._parent._height);
			j.ctx.restore();
		}
	}
}